package com.apotik.legenda.dao;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import org.hibernate.Criteria;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Restrictions;

import com.apotik.legenda.model.Role;
import com.apotik.legenda.util.HibernateUtil;

public class RoleDao {

	
	@SuppressWarnings("unchecked")
	public Role getRole(String roleCode){
		Role result = null;
		
		try {
			Session session = HibernateUtil.getSessionFactory().openSession();
			
			Criteria cr = session.createCriteria(Role.class);
			cr.add(Restrictions.eq("roleCode", roleCode));
			
			List<Role> list = cr.list();
			if(list.size()>0){
				result = list.get(0);
			}
			
		} catch (Exception e) {
			System.out.println("Exception: "+e.getMessage());
		}
		
		return result;
	}
	
	@SuppressWarnings("unchecked")
	public List<Role> loadAll(boolean isActive){
		List<Role> list = new ArrayList<>();
		
		try {
			Session session = HibernateUtil.getSessionFactory().openSession();
			
			Criteria cr = session.createCriteria(Role.class);
			cr.add(Restrictions.eq("isActive", isActive));
			list = cr.list();
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, e.getMessage(), "Exception", JOptionPane.ERROR_MESSAGE);
		}
		
		return list;
	}
}
